/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.dao;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.colswe.testingProject.entity.Person;

/**
 *
 * @author Andrés Esteban Paez Torres
 * @version 1.0
 */
public class PersonDAO implements DAO<Person> {

    private EntityManagerFactory emf;
    private EntityManager em;

    public PersonDAO(EntityManagerFactory emf) {
        this.emf = emf;
    }

    @Override
    public boolean create(Person toCreate) {
        boolean result = true;
        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(toCreate);
            em.getTransaction().commit();
        } catch (Exception ex) {
            result = false;
            System.err.print(ex.getMessage());
            ex.printStackTrace(System.err);
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return result;
    }

    @Override
    public Person read(Person toRead) {
        Person person;
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT u FROM Person "
                    + "AS u WHERE u.userName= :userParam");
            query.setParameter("userParam", toRead.getUserName());
            em.getTransaction().begin();
            person = (Person) query.getSingleResult();
            em.getTransaction().commit();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace(System.err);
            person = null;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return person;
    }

    @Override
    public boolean delete(Person toDelete) {
        boolean result = true;
        try {
            if ((toDelete = read(toDelete)) != null) {
                em = emf.createEntityManager();
                em.getTransaction().begin();
                toDelete = em.getReference(Person.class,
                        toDelete.getIdPerson());
                em.remove(toDelete);
                em.getTransaction().commit();
            } else {
                throw new Exception("The registry does not exist");
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace(System.err);
            if (em.isOpen()) {
                em.getTransaction().rollback();
            }
            result = false;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return result;
    }

    @Override
    public boolean update(Person toUpdate) {
        boolean result = true;
        Person old;
        try {
            if ((old = read(toUpdate)) != null) {
                toUpdate.setIdPerson(old.getIdPerson());
                em = emf.createEntityManager();
                em.getTransaction().begin();
                em.merge(toUpdate);
                em.getTransaction().commit();
            } else {
                throw new Exception("The registry does not exist");
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace(System.err);
            if (em.isOpen()) {
                em.getTransaction().rollback();
            }
            result = false;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return result;
    }

    @Override
    public Collection<Person> readAll() {
        Collection<Person> persons;
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT u FROM Person AS u");
            em.getTransaction().begin();
            persons = query.getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            persons = null;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return persons;
    }
}
